﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace carsitem.xiaoshou
{
    public partial class UpdateSell : Form
    {
        public int sellid;
        public UpdateSell()
        {
            InitializeComponent();
        }

        private void UpdateBt_Click(object sender, EventArgs e)
        {
            //声明控件变量
            string GoodsNumber = this.goodsbianBox.Text;
            string EmployeeID = this.comboxcaozuo.Text;
            string customerID = this.gubianBox.Text;
            string SellCount = this.moneyBox.Text;
            DateTime Selldate = this.dateTimePicker1.Value;
            string SellAddress = this.AddressBox9.Text;
            string Sellstate = this.comboxZT.Text;
            string Sellway = this.comboxFF.Text;
            string Note = this.noteBox.Text;
            //判断控件输入值是否合法
            zhengzebiaoda zda = new zhengzebiaoda();
            bool gb = zda.IsNumber(customerID);
            bool mb = zda.IsNumber(SellCount);
            bool gd = zda.IsNumber(GoodsNumber);
            if (gb == false && gubianBox.Text != "")
            {
                MessageBox.Show("输入信息有误，请重新输入");
            }
            if (mb = false && moneyBox.Text != "")
            {
                MessageBox.Show("输入信息有误，请重新输入");
            }
            if (gd = false && goodsbianBox.Text != "")
            {
                MessageBox.Show("输入信息有误，请重新输入");
            }
            //判断控件值不同输入情况所执行的语句
            string sql = "";
            if (noteBox.Text != "")
            {
                sql = string.Format(@"update sell set GoodsNumber={0},EmployeeID={1},customerID={2},SellCount={3},Selldate='{4}' ,
              SellAddress='={5}',Sellstate='{6}',Sellway='{7}',Note='{8}' where SellID={9}", GoodsNumber, EmployeeID
            , customerID, SellCount, Selldate, SellAddress, Sellstate, Sellway, Note, sellid);
            }
            if (noteBox.Text == "")
            {
                sql = string.Format(@"update sell set GoodsNumber={0},EmployeeID={1},customerID={2},SellCount={3},Selldate='{4}' ,
              SellAddress='={5}',Sellstate='{6}',Sellway='{7}' where SellID={8}", GoodsNumber, EmployeeID
             , customerID, SellCount, Selldate, SellAddress, Sellstate, Sellway, sellid);
            }
            DBHelper db = new DBHelper();

            bool result = db.ExcuteSql(sql);
            if (result == true)
            {
                MessageBox.Show("修改成功");
            }
            else
            {
                MessageBox.Show("修改失败");
            }
        }

        private void UpdateSell_Load(object sender, EventArgs e)
        {
            //下拉列表获取员工编号
            DBHelper dbr = new DBHelper();
            DataTable one = dbr.GetDataTableBySql("select EmployeeID from Sell");
            this.comboxcaozuo.ValueMember = "EmployeeID";
            this.comboxcaozuo.DisplayMember = "EmployeeID";
            this.comboxcaozuo.DataSource = one;
            //判断是否获取到选中的数据
            string sql = string.Format("select * from Sell  where SellID={0}", sellid);
            DataTable dta = dbr.GetDataTableBySql(sql);
            if (dta != null && dta.Rows.Count > 0)
            {
                this.gubianBox.Text = dta.Rows[0]["customerID"].ToString();
                this.comboxcaozuo.Text = dta.Rows[0]["EmployeeID"].ToString();
                this.AddressBox9.Text = dta.Rows[0]["SellAddress"].ToString();
                this.moneyBox.Text = dta.Rows[0]["SellCount"].ToString();
                this.dateTimePicker1.Value = Convert.ToDateTime(dta.Rows[0]["Selldate"]);
                this.goodsbianBox.Text = dta.Rows[0]["GoodsNumber"].ToString();
                this.comboxZT.Text = dta.Rows[0]["Sellstate"].ToString();
                this.comboxFF.Text = dta.Rows[0]["Sellway"].ToString();
            }
        }

        private void chongzhibt_Click(object sender, EventArgs e)
        {
            foreach (Control con in this.Controls)//遍历控件类集合
            {
                TextBox text = con as TextBox;//获取TextBox对象
                RadioButton rdo = con as RadioButton;//获取RadioButton对象
                CheckBox che = con as CheckBox;//获取CheckBox对象
                if (text != null)//文本框清空
                {
                    text.Text = "";
                }
                if (che != null)//多选框全不选
                {
                    che.Checked = false;
                }
            }
        }

        private void tuichubt_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void comboxcaozuo_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    }
}
